package com.jckl.foodathon.widget;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jckl.foodathon.R;
import com.jckl.foodathon.model.BlogsListWidget;

public class BlogsListAdapter extends BaseAdapter{

	Context context;
	List<BlogsListWidget> blogsListWidget;

	public BlogsListAdapter(Context context, List<BlogsListWidget> blogList) {
		this.context = context;
		this.blogsListWidget = blogList;
	}

	@Override
	public int getCount() {
		return blogsListWidget.size();
	}

	@Override
	public Object getItem(int arg0) {
		return blogsListWidget.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return blogsListWidget.indexOf(getItem(arg0));
	}

	private class BlogListHolder {
		ImageView bloggerImage;
		TextView blogTitle;
		TextView blogger;
		TextView postDate;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		BlogListHolder holder = null;

		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.widget_bloglist, null);
			holder = new BlogListHolder();

			holder.bloggerImage = (ImageView) convertView.findViewById(R.id.bloglist_image);
			holder.blogTitle = (TextView) convertView.findViewById(R.id.bloglist_title);
			holder.blogger = (TextView) convertView.findViewById(R.id.bloglist_blogger);
			holder.postDate = (TextView) convertView.findViewById(R.id.bloglist_postdate);
			
			BlogsListWidget row_pos = blogsListWidget.get(position);
			
			holder.bloggerImage.setImageBitmap(row_pos.getBloggerImage());
			holder.blogTitle.setText(row_pos.getBlogTitle());
			holder.blogger.setText(row_pos.getBloggerName());
			holder.postDate.setText(row_pos.getPostDate());
		
			convertView.setTag(holder);
		} else {
			holder = (BlogListHolder) convertView.getTag();
		}
		
		return convertView;
	}

}
